﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Public Class CommandLineParser

    Private Const SERVER As String = "/server:"
    Private Const DATABASE As String = "/database:"
    Private Const USERNAME As String = "/username:"
    Private Const PASSWORD As String = "/password:"
    Private Const OUTPUTXMLFILE As String = "/outputxmlpath:"
    Private Const OUTPUTCODEFILE As String = "/outputcodepath:"
    Private Const ARCHIVEFILE As String = "/archivepath:"
    Private Const GENNAMESPACE As String = "/namespace:"
    Private Const SQLMETALLOC As String = "/sqlmetallocation:"
    Private Const LANGUAGE As String = "/language:"

    Public Sub Parse(ByVal args() As String, ByRef context As AppContext)

        For Each arg As String In args

            Dim lower As String = arg.ToLower

            If lower.StartsWith(SERVER) Then
                context.Server = TrimQuotes(arg.Substring(SERVER.Length))
            End If

            If lower.StartsWith(DATABASE) Then
                context.Database = TrimQuotes(arg.Substring(DATABASE.Length))
            End If

            If lower.StartsWith(USERNAME) Then
                context.UserName = TrimQuotes(arg.Substring(USERNAME.Length))
            End If

            If lower.StartsWith(PASSWORD) Then
                context.Password = TrimQuotes(arg.Substring(PASSWORD.Length))
            End If

            If lower.StartsWith(OUTPUTXMLFILE) Then
                context.OutputXmlFilePath = TrimQuotes(arg.Substring(OUTPUTXMLFILE.Length))
            End If

            If lower.StartsWith(OUTPUTCODEFILE) Then
                context.OutputCodeFilePath = TrimQuotes(arg.Substring(OUTPUTCODEFILE.Length))
            End If

            If lower.StartsWith(ARCHIVEFILE) Then
                context.ArchiveFilePath = TrimQuotes(arg.Substring(ARCHIVEFILE.Length))
            End If

            If lower.StartsWith(GENNAMESPACE) Then
                context.GeneratedNamespace = TrimQuotes(arg.Substring(GENNAMESPACE.Length))
            End If

            If lower.StartsWith(SQLMETALLOC) Then
                context.SqlMetalLocation = TrimQuotes(arg.Substring(SQLMETALLOC.Length))
            End If

            If lower.StartsWith(LANGUAGE) Then
                context.SetLanguage(TrimQuotes(arg.Substring(LANGUAGE.Length)))
            End If

        Next

    End Sub

    Private Function TrimQuotes(ByVal s As String)
        Return s.Trim(""""c)
    End Function

End Class
